package com.ilovepostcard.utils;

import org.json.JSONException;
import org.json.JSONObject;

import com.ilovepostcard.model.Postcard;
import com.ilovepostcard.model.User;

public class JsonUtil {
    
    /*
     "data": { 
            "UserID": 0,         //用户ID，类型：Int64
            "Username": "",       //用户名，类型：String
            "Nickname": "",       //昵称，类型：String
            "IsValidEmail": true,   //邮箱是否已验证，类型：Boolean
            "HasHeadImg": false,        //是否拥有头像(标识用户是否上传过头像) ，类型：Boolean
            "HeadImgUrl": "",      //头像地址（无论是否拥有头像，头像地址都会返回），类型：String
            "Status": 1,         //用户状态(1,正常；2,锁定) ，类型：Int32
            "MyAttentionCount": 0,   //我关注的人数，类型：Int32
            "BeAttentionCount": 0,   //关注我的人数，类型：Int32
            "PublishCount": 0,       //创作的明信片数量，类型：Int32
            "PlatformType": 1,    开放平台类型，1=新浪，2=QQ
            "Introduction": "", //自我介绍，类型：String
            "MySpace": "",  //个人空间URL，类型：String
            "Sex": "", //性别，类型：Int32；0,未知；1,男；2,女；
            "Birthday":"", //生日（没有设置生日时为空字符串），类型：String
            "Province":"", //省份
            "City":""      //城市
              }
     */
    public static User json2User(JSONObject json) throws JSONException {
        User user = new User();
        user.UserID = json.getLong("UserID");
        user.Username = json.getString("Username");
        user.Nickname = json.getString("Nickname");
        user.IsValidEmail = json.getBoolean("IsValidEmail");
        user.HasHeadImg = json.getBoolean("HasHeadImg");
        user.HeadImgUrl = json.getString("HeadImgUrl");
        user.Status = json.getInt("Status");
        user.MyAttentionCount = json.getInt("MyAttentionCount");
        user.BeAttentionCount = json.getInt("BeAttentionCount");
        user.PublishCount = json.getInt("PublishCount");
        user.PlatformType = json.getInt("PlatformType");
        user.Introduction = json.getString("Introduction");
        user.MySpace = json.getString("MySpace");
        user.Sex = json.getInt("Sex");
        user.Birthday = json.getString("Birthday");
        user.Province = json.getString("Province");
        user.City = json.getString("City");
        return user;
    }

    public static Postcard json2Postcard(JSONObject json) throws JSONException {
        Postcard postcard = new Postcard();
        postcard.PostcardID = (json.getInt("PostcardID"));
        postcard.Longitude = (json.getDouble("Longitude"));
        postcard.Latitude = (json.getDouble("Latitude"));
        postcard.Location = json.getString("Location");
        postcard.ToFriendWords = (json.getString("ToFriendWords"));
        postcard.Story = (json.getString("Story"));
        postcard.PublishTime = (json.getString("PublishTime"));
        postcard.Likes = (json.getInt("Likes"));
        postcard.Comments = (json.getInt("Comments"));
        postcard.Favorite = (json.getInt("Favorite"));
        postcard.IsShowWords = (json.getBoolean("IsShowWords"));
        postcard.UserID = (json.getLong("UserID"));
        postcard.IsHorizontal = (json.getBoolean("IsHorizontal"));
        postcard.ThumImgUrl = (json.getString("ThumImgUrl"));
        postcard.PerviewImgUrl = (json.getString("PerviewImgUrl"));
        postcard.ViewPermission = json.getInt("ViewPermission");
        postcard.LocationPermission = json.getInt("LocationPermission");
        postcard.TagIn = json.getString("TagIn");
        postcard.Status = json.getInt("Status");
        return postcard;
    }
    
    public static JSONObject postcard2Json(Postcard postcard) throws JSONException {
        JSONObject json = new JSONObject();
        json.put("PostcardID", postcard.PostcardID);
        json.put("Longitude", postcard.Longitude);
        json.put("Latitude", postcard.Latitude);
        json.put("ToFriendWords", postcard.ToFriendWords);
        json.put("Story", postcard.Story);
        json.put("PublishTime", postcard.PublishTime);
        json.put("Likes", postcard.Likes);
        json.put("Comments", postcard.Comments);
        json.put("Favorite", postcard.Favorite);
        json.put("IsShowWords", postcard.IsShowWords);
        json.put("UserID", postcard.UserID);
        json.put("IsHorizontal", postcard.IsHorizontal);
        json.put("ThumImgUrl", postcard.ThumImgUrl);
        json.put("PerviewImgUrl", postcard.PerviewImgUrl);
        json.put("ViewPermission", postcard.ViewPermission);
        json.put("LocationPermission", postcard.LocationPermission);
        json.put("TagIn", postcard.TagIn);
        json.put("Status", 0);
        return json;
    }
}
